package cate.game.play.buff.h;

import cate.common.table.d.GD;
import cate.common.table.d.GDSkill;
import cate.game.play.buff.BuffHandler;
import cate.game.play.config.PlayCfgArgs;
import cate.game.play.fighter.Fighter;
import cate.game.play.proce.action.ctx.SkillActionCtx;
import easy.java.dev.note.NoteField;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * 受到技能直接攻击时添加buff
 */
public class 受击添加buffBH extends BuffHandler {

	@NoteField("添加配置列表")
	public List<Config> buffAddList = new ArrayList<>();

	public void parse(String code) {
		if (StringUtils.isNotBlank(code)) {
			for (String s : code.split(",")) {
				buffAddList.add(new Config(s));
			}
		}
	}

	//战斗者类型=1&目标基准=1&buff=123&数量=1&概率=10000
	public static class Config {

		@NoteField("战斗者类型")
		private Byte fighterType;

		@NoteField("目标基准")
		public byte targetSrc;

		@NoteField("buff")
		public int buff;

		@NoteField("数量")
		public int num;

		@NoteField("概率")
		public int chance;

		public void parse(String code) {
			PlayCfgArgs args = new PlayCfgArgs(code);
			fighterType = args.getByte("战斗者类型");
			targetSrc = args.getByte("目标基准", GDSkill.Base.TARGET);
			buff = args.getInt("buff", 0);
			num = args.getInt("数量", 1);
			chance = args.getInt("概率", GD.W_10000);
		}

		public Config(String code) {
			parse(code);
		}
	}


	@Override
	public void afterSufferOneAttack(SkillActionCtx action, long totalDamage, boolean strike) {
		for (Config cfg : buffAddList) {
			if (cfg.fighterType != null && cfg.fighterType != action.getActor().type) {
				continue;
			}
			Fighter target = getFighterBySrc(cfg.targetSrc, action.getActor(), buff.owner);
			target.buff.tryAddSome(action, buff.caster, cfg.buff, cfg.num, cfg.chance);
		}
	}
}
